package com.wenda.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenda.admin.core.CatchDataPkg;
import com.wenda.admin.core.NetworkUtils;
import com.wenda.admin.core.Protocol;
import com.wenda.admin.entity.TbFlow;
import com.wenda.admin.mapper.FlowMapper;
import com.wenda.admin.vo.FlowQueryVo;
import com.wenda.admin.vo.Resp;
import com.wenda.admin.vo.StatisticVO;
import jpcap.NetworkInterface;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("flow")
public class FlowController  {

    private static boolean START_ING = false;

    @Autowired
    private FlowMapper flowMapper;

    @RequestMapping("ctrl")
    public String ctrlIndex(FlowQueryVo query,Integer currentPage, Model model){
        if(currentPage == null || currentPage < 1){
            currentPage = 1;
        }
        QueryWrapper<TbFlow> queryWrapper = new QueryWrapper<TbFlow>();
        queryWrapper.orderByDesc("create_time");
        if(query != null){
            if(StringUtils.isNotBlank(query.getStartTime())){
                queryWrapper.ge("create_time",query.getStartTime());
            }
            if(StringUtils.isNotBlank(query.getEndTime())){
                queryWrapper.le("create_time",query.getEndTime());
            }
            if(StringUtils.isNotBlank(query.getStartIp())){
                queryWrapper.eq("start_ip",query.getStartIp());
            }
            if(StringUtils.isNotBlank(query.getEndIp())){
                queryWrapper.eq("to_ip",query.getEndIp());
            }
        }
        IPage<TbFlow> page = new Page<>(currentPage,10);
        page = flowMapper.selectPage(page, queryWrapper);
        model.addAttribute("page",page);
        Map<String,Integer> networks = NetworkUtils.getAllNetworkSeq();
        model.addAttribute("networks",networks);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("query",query);
        model.addAttribute("status",START_ING);
        return "/flow/ctrl";
    }

    @RequestMapping("start")
    @ResponseBody
    public Resp open(String netWorkName){
        if(START_ING){
            return Resp.success("success");
        }
        START_ING = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, NetworkInterface> networks = NetworkUtils.getAllNetwork();
                NetworkInterface net = networks.get(netWorkName);
                try {
                    CatchDataPkg.start(net);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return Resp.success("success");
    }

    @RequestMapping("stop")
    @ResponseBody
    public Resp close(){
        CatchDataPkg.stop();
        START_ING = false;
        return Resp.success("success");
    }

    @RequestMapping("statistic")
    public String statistic(FlowQueryVo query,Model model){
        model.addAttribute("query",query);
        List<StatisticVO> data = flowMapper.statistic(query);
        Map<String,Integer> dataMap = new HashMap<>();
        for(StatisticVO temp:data){
            dataMap.put(temp.getProtocol(),temp.getNum());
        }
        for(Protocol temp:Protocol.values()){
            if(dataMap.get(temp.getName()) == null){
                dataMap.put(temp.getName(),0);
            }
        }
        model.addAttribute("data",dataMap);
        return "/flow/analyse";
    }




}
